package com.hope;

import java.io.FileNotFoundException;
import java.io.InputStream;

import android.support.v4.app.Fragment;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;

public class PillInformation extends Fragment implements EditNumberDialog.DialogClickListener, EditStringDialog.DialogClickListener {
    private final String ARG_OBJECT = "pill_information";
    private Context context;
    private Fragment fragment;
    private TextView pill_name;
    private TextView pill_dosage;
    private TextView pill_number;
    private TextView pill_schedule;
    private TextView pill_time;
    private TextView pill_duration;
    private ImageView pill_image;
    public static final int PILL_NAME = 0;
    public static final int PILL_DOSAGE = 1;
    public static final int PILL_NUMBER = 2;
    public static final int PILL_SCHEDULE = 3;
    public static final int PILL_TIME = 4;
    public static final int PILL_DURATION = 5;
    private final int SELECT_PHOTO = 1;
	
	public PillInformation() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.pill_information, container, false);
        getActivity().setTitle("Pill Information");
        fragment = this;
        context = inflater.getContext();

        pill_name = (TextView) rootView.findViewById(R.id.pill_information_name);
        pill_dosage = (TextView) rootView.findViewById(R.id.pill_information_dosage);
        pill_number = (TextView) rootView.findViewById(R.id.pill_information_number);
        pill_schedule = (TextView) rootView.findViewById(R.id.pill_information_schedule);
        pill_time = (TextView) rootView.findViewById(R.id.pill_information_time);
        pill_duration = (TextView) rootView.findViewById(R.id.pill_information_duration);
        pill_image = (ImageView) rootView.findViewById(R.id.pill_information_image);
        
        Bundle args = getArguments();
        pill_name.setText(args.getString(ARG_OBJECT));
        pill_dosage.setText("2");
        pill_number.setText("20");
        pill_schedule.setText("M, W, F");
        pill_time.setText("8:00 A.M.");
        pill_duration.setText("4 weeks");

        rootView.findViewById(R.id.pill_info_name).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				showStringDialog(PILL_NAME, (String) pill_name.getText(), "Edit Pill Name");
			}
        	
        });
        rootView.findViewById(R.id.pill_info_dosage).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				showNumberDialog(PILL_DOSAGE, (String) pill_dosage.getText(), "Edit Dosage");
			}
        	
        });
        rootView.findViewById(R.id.pill_info_number).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				showNumberDialog(PILL_NUMBER, (String) pill_number.getText(), "Edit Number of Pills");
			}
        	
        });
        rootView.findViewById(R.id.pill_information_schedule).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				showScheduleDialog(PILL_SCHEDULE);
			}
        	
        });
        rootView.findViewById(R.id.pill_information_time).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				showTimeDialog(PILL_TIME, "Edit Time");
			}
        	
        });
        rootView.findViewById(R.id.pill_info_duration).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				showNumberDialog(PILL_DURATION, (String) pill_duration.getText(), "Edit Duration");
			}
        	
        });
        rootView.findViewById(R.id.pill_information_image).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO);
			}
        	
        });
        
        return rootView;
    }
    
    public String getArgObject() {
    	return ARG_OBJECT;
    }

	@Override
	public void onDialogPositiveClick(String s, int i) {
		switch (i) {
			case PILL_NAME: pill_name.setText(s);
			break;
			case PILL_DOSAGE: pill_dosage.setText(s);
			break;
			case PILL_NUMBER: pill_number.setText(s);
			break;
			case PILL_SCHEDULE: pill_schedule.setText(s);
			break;
			case PILL_TIME: pill_time.setText(s);
			break;
			case PILL_DURATION: pill_duration.setText(s);
			break;
		}
	}
	
	private void showNumberDialog(int i, String s, String title) {
		EditNumberDialog dialog = new EditNumberDialog();
        Bundle args = new Bundle();
        args.putInt(dialog.getArgObjectFieldNum(), i);
        args.putString(dialog.getArgObjectField(), s);
        args.putString(dialog.getArgObjectTitle(), title);
        dialog.setArguments(args);
        dialog.setTargetFragment(fragment, 0);
        dialog.show(getFragmentManager(), "EditNumberDialog");
	}
	
	private void showStringDialog(int i, String s, String title) {
		EditStringDialog dialog = new EditStringDialog();
        Bundle args = new Bundle();
        args.putInt(dialog.getArgObjectFieldNum(), i);
        args.putString(dialog.getArgObjectField(), s);
        args.putString(dialog.getArgObjectTitle(), title);
        dialog.setArguments(args);
        dialog.setTargetFragment(fragment, 0);
        dialog.show(getFragmentManager(), "EditStringDialog");
	}
	
	private void showScheduleDialog(int i) {
		
	}
	
	private void showTimeDialog(int i, String t) {
		TimePickerDialog dialog = new TimePickerDialog(context, new OnTimeSetListener() {

			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				String end = "A.M.";
				if (hourOfDay >= 12) {
					hourOfDay -= 12;
					end = "P.M.";
				}
				if (hourOfDay == 0) {
					hourOfDay += 12;
				}
				String min = "" + minute;
				if (min.length() == 1) {
					min = "0" + min;
				}
				pill_time.setText(hourOfDay + ":" + min + " " + end);
			}
			
		}, 0, 0, false);
		dialog.setTitle(t);
		dialog.show();
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
 
        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == -1){
                try {
                    final Uri imageUri = imageReturnedIntent.getData();
                    final InputStream imageStream = context.getContentResolver().openInputStream(imageUri);
                    final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                    pill_image.setImageBitmap(selectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
 
            }
        }
	}
}